<?php


class Photos_adminController extends Controller {
    protected $allowed = array('administrator');

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        if ($this->isAllowed()) {
            $photos = Model::loadRowsFromQuery("SELECT * FROM photo;", "PhotoModel");
            $this->render("admin/photos/index" , array('photos' => $photos));
        }
        else {
            $this->showError('You are not allowed to open this page');
        }
    }

    public function delete($photo_id) {
        if ($this->isAllowed()) {
            $photo = Model::factory('photo' , $photo_id);
            if ($photo->loaded() && $this->isAllowed()) {
                $photo->delete();
            }
            
            $users = Model::loadRowsFromQuery("SELECT * FROM `user` WHERE `photo_id` = ".$photo_id.";" , 'AuthModel');
            foreach ($users as $user) {
				$user->photo_id = NULL;
				$user->save();
			}
            
            $this->redirect($this->createUrl('photos_admin/index'));
        }
        else
            $this->showError('You are not allowed to open this page');
    }
}


?>
